//
//  ReleaseViewController.swift
//  DynamicStateLJZ
//
//  Created by kys-11 on 2019/5/8.
//  Copyright © 2019年 xl123. All rights reserved.
//

import UIKit
import MBProgressHUD
class ReleaseViewController: UIViewController,photoctionDelegate {
    typealias sucess = (_ success:String) -> Void
    var publishview: PublishUIView!
    var image:[UIImage] = []//
    var imageString:[String] = []
    var usernameString:String!//s
    var contentString:String!//上传的neir
    var userimage:UIImage!//头像
    var userimageStr:String!//头像转换的字符串
    var HUD:MBProgressHUD!
    
    override func viewDidLoad() {
        super.viewDidLoad()

        self.view.backgroundColor = UIColor(displayP3Red: 11.76, green: 11.76, blue: 11.76, alpha: 0.8)
        self.title = "说说"
    
        let rightButton = UIBarButtonItem (title: "发表", style: .plain, target: self,
                                           action:#selector(releaction))
        //        rightButton.image = UIImage(named: "headimage2.jpg")
        
        self.navigationItem.rightBarButtonItem = rightButton
     
        publishview = PublishUIView()
        publishview.frame = CGRect(x: 0, y: 64, width: Globalwidth, height: Globalheight)

        self.view.addSubview(publishview)
        publishview.photodlegate = self
        //上传图片代理
    }
    

//上传执行方法
    @objc func releaction(){
        
        usernameString = publishview.pubLbale.text
        contentString = publishview.pubtextView.text
        
        userimage = publishview.pubTopimage.image
        // 将图片转化成Data
        let imageDate = userimage.pngData()
      
            //先进行判断
            if (self.contentString.count != 0){
                //将数据上传
                BmobAction.BomDate(classname: "dyname", content: self.contentString, imageArray: self.imageString, success: { (result) in
                    print("上传成功")
                    //跳转到动态界面
                    self.navigationController?.pushViewController(DynamicStateLController(), animated: true)
                }) { (error) in
                    print(error)
                }
            }
                //判断为空处理
            else{
                self.HUD = MBProgressHUD()
                self.HUD.offset.x = 0
                self.HUD.offset.y = 0
                self.HUD.mode = MBProgressHUDMode.text
                self.HUD.label.text = "请输入内容或添加图片"
                self.view.addSubview(self.HUD)
                self.HUD.show(animated: true)
                self.HUD.hide(animated: true, afterDelay: 3)
                
            }
        }//将y头像上c传到七牛
       
        
    

    
    //选择图片的代理d方法
    func photoction(identifying: String) {
        _ = self .presentHGImagePicker(maxSelected:9 - image.count) { (assets) in
            //结果处理
            print ("共选择了\(assets.count)张图片，分别如下：")
            for asset in assets {

                self.image.append(ImageConversion.PHAssetToUIImage(imag: asset))
            }
            self.publishview.renewalimage(images: self.image)
            //将上传的图片
            for img in self.image{
                // 将图片转化成Data
                let imageData = img.pngData()
                
//                // 将Data转化成 base64的字符串
//                let imageBase64String = imageData?.base64EncodedString()
//                //将字符串存到数组里
//                self.imageString.append(imageBase64String!)
                                //将图片上传到七牛上
                self.qiniuAction(imagedate: imageData!, sucess: { (str) in
                    self.imageString.append(str)
                })
                
            }
           
        }
     
    }

    //点击屏幕空白代理执行的函数
    func clickBegan(textview:UITextView){
    textview.resignFirstResponder()
    }
    
 
    
    //上传到七牛的方法
    func qiniuAction(imagedate:Data,sucess:@escaping sucess){
        QiNiuToolsljz().uploadImageData(image: imagedate, result: { (a, usi) in
            print(a)//打印上传进度
            if usi != nil{
                let urlq = usi!
                print(urlq)
              //self.imageString.append(urlq) //将网址串存到数组里
                sucess(urlq)
            }
            
        })
    }
}
